home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / xgccache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-02  |  9.4 KB  |  331 lines

  1. /* Efficient caching of X GCs (graphics contexts).
  2.    Copyright (C) 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: Not in FSF. */
  21.  
  22. /* Emacs uses a lot of different display attributes; for example, assume
  23.    that only four fonts are in use (normal, bold, italic, and bold-italic).
  24.    Then assume that one stipple or background is used for text selections,
  25.    and another is used for highlighting mousable regions.  That makes 16
  26.    GCs already.  Add in the fact that another GC may be needed to display
  27.    the text cursor in any of those regions, and you've got 32.  Add in 
  28.    more fonts, and it keeps increasing exponentially.
  29.  
  30.    We used to keep these GCs in a cache of merged (fully qualified) faces.
  31.    However, a lot of other code in xterm.c used XChangeGC of existing GCs,
  32.    which is kind of slow and kind of random.  Also, managing the face cache
  33.    was tricky because it was hard to know when a face was no longer visible
  34.    on the frame -- we had to mark all frames as garbaged whenever a face
  35.    was changed, which caused an unpleasant amount of flicker (since faces are
  36.    created/destroyed (= changed) whenever a frame is created/destroyed.
  37.  
  38.    So this code maintains a cache at the GC level instead of at the face 
  39.    level.  There is an upper limit on the size of the cache, after which we
  40.    will stop creating GCs and start reusing them (reusing the least-recently-
  41.    used ones first).  So if faces get changed, their GCs will eventually be
  42.    recycled.  Also more sharing of GCs is possible.
  43.  
  44.    This code uses hashtables.  It could be that, if the cache size is small
  45.    enough, a linear search might be faster; but I doubt it, since we need
  46.    `equal' comparisons, not `eq', and I expect that the optimal cache size
  47.    will be ~100.
  48.  
  49.    Written by jwz, 14 jun 93
  50.  */
  51.  
  52. #include <config.h>
  53. #include <X11/Xlib.h>
  54. #include "xgccache.h"
  55.  
  56.  
  57. #define GC_CACHE_SIZE 100
  58.  
  59. #define GCCACHE_HASH
  60.  
  61.  
  62. #ifdef GCCACHE_HASH
  63. #include "lisp.h"
  64. #include "hash.h"
  65. #endif
  66.  
  67. struct gcv_and_mask {
  68.   XGCValues gcv;
  69.   unsigned long mask;
  70. };
  71.  
  72. struct gc_cache_cell {
  73.   GC gc;
  74.   struct gcv_and_mask gcvm;
  75.   struct gc_cache_cell *prev, *next;
  76. };
  77.  
  78. struct gc_cache {
  79.   Display *dpy;        /* used only as arg to XCreateGC/XFreeGC */
  80.   Window window;    /* used only as arg to XCreateGC */
  81.   int size;
  82.   struct gc_cache_cell *head;
  83.   struct gc_cache_cell *tail;
  84. #ifdef GCCACHE_HASH
  85.   c_hashtable table;
  86. #endif
  87.  
  88.   int create_count;
  89.   int delete_count;
  90. };
  91.  
  92. #ifdef GCCACHE_HASH
  93. static unsigned long 
  94. gc_cache_hash (CONST void *arg)
  95. {
  96.   struct gcv_and_mask *gcvm = (struct gcv_and_mask *) arg;
  97.   unsigned long *longs = (unsigned long *) &gcvm->gcv;
  98.   unsigned long hash = gcvm->mask;
  99.   int i;
  100.   /* This could look at the mask and only use the used slots in the
  101.      hash code.  That would win in that we wouldn't have to initialize
  102.      every slot of the gcv when calling gc_cache_lookup.  But we need
  103.      the hash function to be as fast as possible; some timings should
  104.      be done. */
  105.   for (i = 0; i < (sizeof (XGCValues) / sizeof (unsigned long)); i++)
  106.     hash = (hash<<1) ^ *longs++;
  107.   return hash;
  108. }
  109.  
  110. #endif /* GCCACHE_HASH */
  111.  
  112. static int 
  113. gc_cache_eql (CONST void *arg1, CONST void *arg2)
  114. {
  115.   /* See comment in gc_cache_hash */
  116.   return (!memcmp (arg1, arg2, sizeof (struct gcv_and_mask)));
  117. }
  118.  
  119. struct gc_cache *
  120. make_gc_cache (Display *dpy, Window window)
  121. {
  122.   struct gc_cache *cache =
  123.     (struct gc_cache *) xmalloc (sizeof (struct gc_cache));
  124.   cache->dpy = dpy;
  125.   cache->window = window;
  126.   cache->size = 0;
  127.   cache->head = cache->tail = 0;
  128.   cache->create_count = cache->delete_count = 0;
  129. #ifdef GCCACHE_HASH
  130.   cache->table =
  131.     make_general_hashtable (GC_CACHE_SIZE, gc_cache_hash, gc_cache_eql);
  132. #endif
  133.   return cache;
  134. }
  135.  
  136. void
  137. free_gc_cache (struct gc_cache *cache)
  138. {
  139.   struct gc_cache_cell *rest, *next;
  140.   rest = cache->head;
  141.   while (rest)
  142.     {
  143.       XFreeGC (cache->dpy, rest->gc);
  144.       next = rest->next;
  145.       xfree (rest);
  146.       rest = next;
  147.     }
  148. #ifdef GCCACHE_HASH
  149.   free_hashtable (cache->table);
  150. #endif
  151.   xfree (cache);
  152. }
  153.  
  154. GC
  155. gc_cache_lookup (struct gc_cache *cache, XGCValues *gcv, unsigned long mask)
  156. {
  157.   struct gc_cache_cell *cell, *next, *prev;
  158.   struct gcv_and_mask gcvm;
  159.  
  160.   if ((!!cache->head) != (!!cache->tail)) abort ();
  161.   if (cache->head && (cache->head->prev || cache->tail->next)) abort ();
  162.  
  163.   gcvm.mask = mask;
  164.   gcvm.gcv = *gcv;    /* this copies... */
  165.  
  166. #ifdef GCCACHE_HASH
  167.  
  168.   if (gethash (&gcvm, cache->table, (void *) &cell))
  169.  
  170. #else /* !GCCACHE_HASH */
  171.  
  172.   cell = cache->tail;    /* start at the end (most recently used) */
  173.   while (cell)
  174.     {
  175.       if (gc_cache_eql (&gcvm, &cell->gcvm))
  176.     break;
  177.       else
  178.     cell = cell->prev;
  179.     }
  180.  
  181.   /* #### This whole file needs some serious overhauling. */
  182.   if (!(mask | GCTile) && cell->gc->values.tile)
  183.     cell = 0;
  184.   else if (!(mask | GCStipple) && cell->gc->values.stipple)
  185.     cell = 0;
  186.  
  187.   if (cell)
  188.  
  189. #endif /* !GCCACHE_HASH */
  190.  
  191.     {
  192.       /* Found a cell.  Move this cell to the end of the list, so that it
  193.      will be less likely to be collected than a cell that was accessed
  194.      less recently.
  195.        */
  196.       if (cell == cache->tail)
  197.     return cell->gc;
  198.  
  199.       next = cell->next;
  200.       prev = cell->prev;
  201.       if (prev) prev->next = next;
  202.       if (next) next->prev = prev;
  203.       if (cache->head == cell) cache->head = next;
  204.       cell->next = 0;
  205.       cell->prev = cache->tail;
  206.       cache->tail->next = cell;
  207.       cache->tail = cell;
  208.       if (cache->head == cell) abort ();
  209.       if (cell->next) abort ();
  210.       if (cache->head->prev) abort ();
  211.       if (cache->tail->next) abort ();
  212.       return cell->gc;
  213.     }
  214.  
  215.   /* else, cache miss. */
  216.  
  217.   if (cache->size == GC_CACHE_SIZE)
  218.     /* Reuse the first cell on the list (least-recently-used).
  219.        Remove it from the list, and unhash it from the table.
  220.      */
  221.     {
  222.       cell = cache->head;
  223.       cache->head = cell->next;
  224.       cache->head->prev = 0;
  225.       if (cache->tail == cell) cache->tail = 0; /* only one */
  226.       XFreeGC (cache->dpy, cell->gc);
  227.       cache->delete_count++;
  228. #ifdef GCCACHE_HASH
  229.       remhash (&cell->gcvm, cache->table);
  230. #endif
  231.     }
  232.   else if (cache->size > GC_CACHE_SIZE)
  233.     abort ();
  234.   else
  235.     {
  236.       /* Allocate a new cell (don't put it in the list or table yet).
  237.        */
  238.       cell = (struct gc_cache_cell *) xmalloc (sizeof (struct gc_cache_cell));
  239.       cache->size++;
  240.     }
  241.  
  242.   /* Now we've got a cell (new or reused).  Fill it in. */
  243.   memcpy (&cell->gcvm.gcv, gcv, sizeof (XGCValues));
  244.   cell->gcvm.mask = mask;
  245.  
  246.   /* Put the cell on the end of the list. */
  247.   cell->next = 0;
  248.   cell->prev = cache->tail;
  249.   if (cache->tail) cache->tail->next = cell;
  250.   cache->tail = cell;
  251.   if (! cache->head) cache->head = cell;
  252.  
  253.   cache->create_count++;
  254. #ifdef GCCACHE_HASH
  255.   /* Hash it in the table */
  256.   puthash (&cell->gcvm, cell, cache->table);
  257. #endif
  258.  
  259.   /* Now make and return the GC. */
  260.   cell->gc = XCreateGC (cache->dpy, cache->window, mask, gcv);
  261.  
  262.   /* debug */
  263.   assert (cell->gc == gc_cache_lookup (cache, gcv, mask));
  264.  
  265.   return cell->gc;
  266. }
  267.  
  268.  
  269. #if 1
  270.  
  271. #include <stdio.h>
  272.  
  273. void describe_gc_cache (struct gc_cache *cache);
  274. void
  275. describe_gc_cache (struct gc_cache *cache)
  276. {
  277.   int count = 0;
  278.   struct gc_cache_cell *cell = cache->head;
  279.   stderr_out ("\nsize:    %d", cache->size);
  280.   stderr_out ("\ncreated: %d", cache->create_count);
  281.   stderr_out ("\ndeleted: %d", cache->delete_count);
  282.   while (cell)
  283.   {
  284.     struct gc_cache_cell *cell2;
  285.     int i = 0;
  286.     stderr_out ("\n%d:\t0x%lx  GC: 0x%08lx  hash: 0x%08lx\n",
  287.          count, (long) cell, (long) cell->gc, gc_cache_hash (&cell->gcvm));
  288.     for (cell2 = cache->head; cell2; cell2 = cell2->next, i++)
  289.       if (count != i &&
  290.       gc_cache_hash (&cell->gcvm) == gc_cache_hash (&cell2->gcvm))
  291.     stderr_out ("\tHASH COLLISION with cell %d\n", i);
  292.     stderr_out ("\tmask:       %8lx\n", cell->gcvm.mask);
  293. #define F(x) (int)cell->gcvm.gcv.x
  294. #define G(w,x) if (F(x) != (~0)) stderr_out ("\t%-12s%8x\n", w, F(x))
  295.     G("function:", function);
  296.     G("plane_mask:", plane_mask);
  297.     G("foreground:", foreground);
  298.     G("background:", background);
  299.     G("line_width:", line_width);
  300.     G("line_style:", line_style);
  301.     G("cap_style:", cap_style);
  302.     G("join_style:", join_style);
  303.     G("fill_style:", fill_style);
  304.     G("fill_rule:", fill_rule);
  305.     G("arc_mode:", arc_mode);
  306.     G("tile:", tile);
  307.     G("stipple:", stipple);
  308.     G("tsx_origin:", ts_x_origin);
  309.     G("tsy_origin:", ts_y_origin);
  310.     G("font:", font);
  311.     G("subwindow:", subwindow_mode);
  312.     G("gexposures:", graphics_exposures);
  313.     G("clip_x:", clip_x_origin);
  314.     G("clip_y:", clip_y_origin);
  315.     G("clip_mask:", clip_mask);
  316.     G("dash_off:", dash_offset);
  317. #undef F
  318. #undef G
  319.     count++;
  320.     if (cell->next && cell == cache->tail)
  321.       stderr_out ("\nERROR!  tail is here!\n\n");
  322.     else if (!cell->next && cell != cache->tail)
  323.       stderr_out ("\nERROR!  tail is not at the end\n\n");
  324.     cell = cell->next;
  325.   }
  326.   if (count != cache->size)
  327.     stderr_out ("\nERROR!  count should be %d\n\n", cache->size);
  328. }
  329.  
  330. #endif
  331.